package robots.behaviour;

import java.util.Collections;
import java.util.List;

import maze.Coordinate;
import maze.Direction;
import maze.Location;
import maze.Maze;

public class MazeCorrector implements Strategy {
	private Maze maze;
	private Coordinate currentCoordinate;
	private Direction previousDirection;
	public MazeCorrector(Maze maze){
		this.maze=maze;
		previousDirection=Direction.NORTH;
	}
	@Override
	public Direction nextDirection(Location loc, Maze[] mazes, Coordinate[] ghosts) {
		currentCoordinate=maze.getCoordinateOfLocation(loc);
		List<Direction> directions=maze.getClearDirections(currentCoordinate.getX(),currentCoordinate.getY(),true);
		for(int i=0; i<directions.size(); i++){
			//System.out.println("dirSize"+directions.size());
			Coordinate coord = Coordinate.getCoordinateInDirection(currentCoordinate, directions.get(i));
			for(int j=0; j<mazes.length;j++){
				if(coord.equals(ghosts[j])){
					directions.remove(i);
					i--;
					break;
				}
			}
		}
		if(directions.contains(previousDirection.right())){
			Direction result=previousDirection.right();
			previousDirection=result;
			return result;
		}
		else if(directions.contains(previousDirection))
			return previousDirection;
		else if(directions.contains(previousDirection.left())){
			Direction result=previousDirection.left();
			previousDirection=result;
			return result;
		}
		Direction result=previousDirection.opposite();
		previousDirection=result;
		return result;
	}
	@Override
	public boolean exploreEverything() {
		// TODO Auto-generated method stub
		return true;
	}
	@Override
	public void setCoordinatePacman(Coordinate coordinate) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String toString(){
		return "MazeCorrector";
	}
	@Override
	public void unsetPacman() {
		// TODO Auto-generated method stub
		
	}
	
	public Direction getPreviousDirection() {
		return previousDirection;
	}
	
	public void setPreviousDirection(Direction dir) {
		previousDirection = dir;
	}
	
}
